package com.xjrsoft.module.oa.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.common.Enum.DeleteMarkEnum;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.module.base.entity.XjrBaseUser;
import com.xjrsoft.module.base.service.IXjrBaseUserService;
import com.xjrsoft.module.oa.entity.XjrOaMessage;
import com.xjrsoft.module.oa.entity.XjrOaMessagestatus;
import com.xjrsoft.module.oa.mapper.XjrOaMessageMapper;
import com.xjrsoft.module.oa.mapper.XjrOaMessagestatusMapper;
import com.xjrsoft.module.oa.service.IXjrOaMessagestatusService;
import com.xjrsoft.module.oa.vo.MessageVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwq
 * @since 2021-05-18
 */
@Service
@AllArgsConstructor
public class XjrOaMessagestatusServiceImpl extends ServiceImpl<XjrOaMessagestatusMapper, XjrOaMessagestatus> implements IXjrOaMessagestatusService {


    private IXjrBaseUserService xjrBaseUserService;

    private XjrOaMessageMapper xjrOaMessageMapper;
    
    

    //将单条消息状态改为已读
    @Override
    public boolean readMessage(String id) {
        XjrOaMessagestatus messagestatus = this.getById(id);
        if(messagestatus==null){
          return false;
        }
        messagestatus.setStatus(10);
        return this.updateById(messagestatus);
    }


    //将所有未读消息状态改为已读
    @Override
    public boolean readAllMessage(Integer module) throws Exception {
        String userId = SecureUtil.getUserId();
        List<Integer> modules = new ArrayList<>();
        modules.add(module);
        if(module==13){
            //添加流程完成的
            modules.add(16);
        }
        List<MessageVo> meassageList = xjrOaMessageMapper.getMeassageList(userId, modules, null, 11);
        Set<String> ids = meassageList.stream().map(MessageVo::getId).collect(Collectors.toSet());
        List<XjrOaMessagestatus> xjrOaMessagestatuses = this.listByIds(ids);
        if(xjrOaMessagestatuses==null||xjrOaMessagestatuses.size()==0){
            throw new Exception("操作异常!!");
        }
        //将状态改为已读
         xjrOaMessagestatuses.forEach(xjrOaMessagestatus -> xjrOaMessagestatus.setStatus(10));
         return this.updateBatchById(xjrOaMessagestatuses);
    }


    //添加消息
    @Override
    public boolean addMessageStatus(XjrOaMessage xjrOaMessage,List<String> userids){
        //判断是否为流程相关消息
        if(xjrOaMessage==null){
            return false;
        }
        //新增消息
        xjrOaMessageMapper.insert(xjrOaMessage);
        Integer module = xjrOaMessage.getModule();
        if(module==10||module==11){
            //将消息发送给每一个用户
            ArrayList<XjrOaMessagestatus> xjrOaMessagestatuses = new ArrayList<>();
            List<XjrBaseUser> xjrBaseUsers = xjrBaseUserService.list();
            for (XjrBaseUser xjrBaseUser : xjrBaseUsers) {
                XjrOaMessagestatus xjrOaMessagestatus = new XjrOaMessagestatus();
                xjrOaMessagestatus.setMessageId(xjrOaMessage.getId()).setUserId(xjrBaseUser.getUserId()).setStatus(11).setSendTime(new Date())
                        .setDeleteMark(DeleteMarkEnum.NODELETE.getCode()).setCreateDate(new Date()).setCreateUserId(SecureUtil.getUserId());
                xjrOaMessagestatuses.add(xjrOaMessagestatus);
            }
            return this.saveBatch(xjrOaMessagestatuses);
        }else {
            //将消息发送给一个人
            ArrayList<XjrOaMessagestatus> xjrOaMessagestatusList = new ArrayList<>();
            for (String userid : userids) {
                XjrOaMessagestatus xjrOaMessagestatus = new XjrOaMessagestatus();
                xjrOaMessagestatus.setMessageId(xjrOaMessage.getId()).setUserId(userid).setStatus(11).setSendTime(new Date())
                        .setDeleteMark(DeleteMarkEnum.NODELETE.getCode()).setCreateDate(new Date()).setCreateUserId(SecureUtil.getUserId());
                xjrOaMessagestatusList.add(xjrOaMessagestatus);
            }
            return this.saveBatch(xjrOaMessagestatusList);
        }
    }

    //删除消息状态
    @Override
    public boolean deleteMessagestatusByIds(List<String> messageIds) {
        return this.remove(Wrappers.<XjrOaMessagestatus>query().lambda().in(XjrOaMessagestatus::getMessageId, messageIds).eq(XjrOaMessagestatus::getDeleteMark, DeleteMarkEnum.NODELETE.getCode()));
    }

    @Override
    public int unreadCount(String userId) {
        List<Integer> modules = new ArrayList<>();
        modules.add(10);
        modules.add(11);
        modules.add(13);
        modules.add(14);
        modules.add(16);
        Integer count = xjrOaMessageMapper.getMeassageUnreadCount(userId, modules);
        Integer schedulesCount = xjrOaMessageMapper.getScheduleMeassageUnreadCount(userId, 12);
        return count+ schedulesCount;
    }
}
